package com.andyliu.advice;

import com.andyliu.common.DateUtil;
import com.andyliu.sys.pojo.SActionLog;
import com.andyliu.sys.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;
import java.util.Date;

/**
 * ClassName: LogAdvice
 * Description: TODO
 * date: 2021/1/20 5:54 下午
 *
 * @author andyliu
 * @since JDK 1.8
 */
//@Component
//@Aspect
public class LogAdvice {
    @Autowired
    private LogService logService;

    private long beginDate;
    private long visit;
    private String baseUrl="";
    private Class targetClass;
    private Method curMethod;
    @Before("a()")
    public void beforeMethod(JoinPoint joinPoint) throws NoSuchMethodException {
        this.beginDate = System.currentTimeMillis();
        // 获取目标对象
        Object target = joinPoint.getTarget();
        // 目标类的类对象
        targetClass = target.getClass();
        String methodName = joinPoint.getSignature().getName();
        // 获取当前方法的参数
        Object[] args = joinPoint.getArgs();
        if(args==null || args.length==0){
            curMethod = targetClass.getDeclaredMethod(methodName);
        }else{
            Class[] argClass= new Class[args.length];
            boolean tflag = false;
            for(int i=0;i<args.length;i++){
                argClass[i]=args[i].getClass();
//                if(args[i].getClass() == HttpSession.class ||
//                    args[i].getClass().isAssignableFrom(HttpSession.class)){
//                    tflag = true;
//                }
            }
            if(!"verification".equals(methodName)) {
                curMethod = targetClass.getDeclaredMethod(methodName, argClass);
            }
        }


    }
    @After("a()")
    public void afterMethod(JoinPoint joinpoint){
        baseUrl="";
        // 获取请求访问时间
        visit = System.currentTimeMillis()-beginDate;
        if(targetClass!=null && curMethod!=null && targetClass!=this.getClass()){
            //  获取类上的requestMapping
            RequestMapping classAnnotation =(RequestMapping)targetClass.getAnnotation(RequestMapping.class);
            if(classAnnotation!=null){
                String[] annotationValues = classAnnotation.value();
                if(annotationValues!=null && annotationValues.length>0){
                    String basePath = annotationValues[0];
                    if(!basePath.startsWith("/")){
                        basePath="/"+basePath;
                    }
                    baseUrl+=basePath;
                }
            }
            // 获取当前请求的地址
            String[] methodPaths =null;
            Object methodAnnotation  =null;
            if(curMethod==null){
                return;
            }
            if(methodAnnotation==null) {
                methodAnnotation = (Object) curMethod.getAnnotation(RequestMapping.class);
                if(methodAnnotation!=null){
                    methodPaths = ((RequestMapping) methodAnnotation).value();
                }
            }
            if(methodAnnotation==null){
                methodAnnotation = (Object)curMethod.getAnnotation(GetMapping.class);
                if(methodAnnotation!=null){
                    methodPaths = ((GetMapping) methodAnnotation).value();
                }
            }
            if(methodAnnotation==null){
                methodAnnotation = (Object)curMethod.getAnnotation(DeleteMapping.class);
                if(methodAnnotation!=null){
                    methodPaths = ((DeleteMapping) methodAnnotation).value();
                }
            }
            if(methodAnnotation==null){
                methodAnnotation = (Object)curMethod.getAnnotation(PutMapping.class);
                if(methodAnnotation!=null){
                    methodPaths = ((PutMapping) methodAnnotation).value();
                }
            }
            if(methodAnnotation!=null){
                if(methodPaths!=null && methodPaths.length>0){
                    String methodUrl=methodPaths[0];
                    if(!methodUrl.startsWith("/")){
                        methodUrl="/"+methodUrl;
                    }
                    baseUrl+=methodUrl;
                }
            }

        }
        System.out.println(baseUrl+":"+visit);
        SActionLog log = new SActionLog();
        log.setUrl(baseUrl);
        log.setVisit(Integer.valueOf((int) visit));
        log.setVisitDate(DateUtil.DateToString(new Date(),"yyyy-MM-dd HH:mm:ss"));
        logService.saveActionLog(log);
    }

    @Pointcut(value = "execution(* com.andyliu.sys.controller.*.*(..))")
    public void a(){

    }
}
